//
//  XGSettingTableViewController.swift
//  TodayNews
//
//  Created by monkey on 2020/2/8.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import Kingfisher
import SCLAlertView
import WebKit

class XGSystemSettingTableViewController: XGBaseSettingViewController
{
    // MARK: - 控制器生命周期方法
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        navigationItem.title = "系统设置"
        // 计算缓存尺寸
        calculateDiskStorageSize()
    }
    
    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)
        
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }
}

// MARK: - 扩展

extension XGSystemSettingTableViewController
{
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        tableView.deselectRow(at: indexPath, animated: true)
        
        if indexPath.section == 0 && indexPath.row == 0 {
            // 账号安全
            let settingViewController = XGAccountSettingViewController(plistName: "TodayNews_account_setting.plist")
            navigationController?.pushViewController(settingViewController, animated: true)
        } else if indexPath.section == 1 && indexPath.row == 0 {
            // 清除缓存
            clearCache()
        } else if indexPath.section == 2 && indexPath.row == 0 {
            // 离线下载
            let offlineDownloadViewController = XGOfflineDownloadViewController()
            navigationController?.pushViewController(offlineDownloadViewController, animated: true)
        }
    }
    
    /// 计算缓存尺寸
    private func calculateDiskStorageSize() -> Void
    {
        let indexPath = IndexPath(row: 0, section: 1)
        guard var itemModel = itemModelWithIndexPath(indexPath) else {
            return
        }
        
        // 计算
        KingfisherManager.shared.cache.calculateDiskStorageSize { (result) in
            switch result {
            case .success(let size):
                let str = String(format: "%.2f MB", CGFloat(size) / 1024.0 / 1024.0)
                itemModel.rightTitle = str
                self.updateItemModel(indexPath: indexPath, itemModel: itemModel)
            case .failure(_):
                XGPrint("计算缓存大小失败")
            }
        }
    }
    
    /// 清除缓存
    private func clearCache() -> Void
    {
        let alert = SCLAlertView()
        alert.addButton("确定") { [weak self] in
            let indexPath = IndexPath(row: 0, section: 1)
            guard var itemModel = self?.itemModelWithIndexPath(indexPath) else {
                return
            }
            
            KingfisherManager.shared.cache.clearDiskCache()
            KingfisherManager.shared.cache.clearMemoryCache()
            itemModel.rightTitle = "0.00 MB"
            let websiteDataTypes = WKWebsiteDataStore.allWebsiteDataTypes()
            let dateFrom = Date(timeIntervalSince1970: 0)
            WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes, modifiedSince: dateFrom) {
                self?.updateItemModel(indexPath: indexPath, itemModel: itemModel)
            }
        }
        
        alert.showInfo("提示", subTitle: "确定要清除缓存吗?")
    }
}
